#include <iostream>
#include <zlib/eventsystem/I_EventSystem.h>
#include <unistd.h>

using namespace std;


Ptr<ProxyMutex> ptr;

void *fun(void *arg)
{
	EThread *thread = new EThread(1);
	sleep(3);
	MUTEX_TRY_LOCK(lock, ptr, thread);
	if(lock.is_locked()) {
		cout << "fun: lock success" << endl;
	}else{
		cout << "fun: lock faild" << endl;
	}

	return NULL;
}

int main()
{
	ink_thread t;
	ptr = new_ProxyMutex();
	EThread *thread = new EThread(2);

	MUTEX_TAKE_TRY_LOCK(ptr, thread);
	cout << "tack block lock success" << endl;
	MUTEX_TAKE_UNLOCK(ptr, thread);
	cout << "tack unlock lock" << endl;

	t = ink_thread_create(fun, NULL, 0);

	MUTEX_TAKE_TRY_LOCK(ptr, thread);
	MUTEX_TAKE_TRY_LOCK(ptr, thread);
	MUTEX_TAKE_TRY_LOCK(ptr, thread);
	cout << "multi tack block lock success" << endl;
	sleep(3);
	MUTEX_TAKE_UNLOCK(ptr, thread);
	MUTEX_TAKE_UNLOCK(ptr, thread);
	MUTEX_TAKE_UNLOCK(ptr, thread);
	
	ink_thread_join(t, NULL);
	MUTEX_LOCK(l, ptr, thread);
	cout << "auto take mutex success" << endl;

	return 0;
}
